home *** CD-ROM | disk | FTP | other *** search
/ Technotools / Technotools (Chestnut CD-ROM)(1993).ISO / misc_pto / loadall / loadall.doc
Text File  |  1990-07-15  |  11KB  |  222 lines

  1.       To:  All                        Message #:  7666
  2.     From:  Andy Vaught                Submitted:  13 Jul 90  9:41:00
  3.  Subject:  LOADALL, 1/4                  Status:  Public
  4. Received:  No                             Group:  80XXX (30)
  5.  
  6. Article 90 of comp.sys.intel:
  7.  Path: bilver!rtmvax!peora!ge-dab!steinmetz!uunet!ccicpg!turnkey!conexch!root
  8.  From: root@conexch.UUCP (Larry Dighera)
  9.  Newsgroups: comp.sys.intel
  10.  Subject: Re: Returning the 80286 to Real Mode
  11.  Summary: Have you heard about the LOADALL instruction?
  12.  Message-ID: <11004@conexch.UUCP>
  13.  Date: 31 Oct 88 14:05:04 GMT
  14.  References: <1081@xenon.idec.stc.co.uk>
  15.  Reply-To: root@conexch.UUCP (Larry Dighera)
  16.  Organization: The Consultants' Exchange, Orange County, CA.  (714) 842-6348
  17.  
  18.  In article <1081@xenon.idec.stc.co.uk> sanders@idec.stc.co.uk (Tom Sanders)
  19.  writes:
  20.  >
  21.  >   Rumour has it that it is possible to switch the 80286 back to real mode
  22.  >   once having gone protected.  Can anyone advise me how this is done?
  23.  >
  24.  
  25.  
  26.  Here is a copy of an article that recently appeared in a periodical magizine.
  27.  The informatin it contains and the opinions expressed in it are not mine.
  28.  I'm sure you will find it useful and informitive.
  29.  
  30.  
  31.  
  32.  ------------------------  LOADALL DOCUMENTATION  -------------------------
  33.  
  34.  Secret 286 LOADALL instruction allows access to extended memory in real mode.
  35.  
  36.  In last month's article on the P9, we described the method used to access
  37.  extended memory (memory beyond the lower 1 megabyte) from real-mode
  38.  programs running on a 286.  This method requires switching to protected
  39.  mode to perform access, and then resetting the processor to return to
  40.  real mode.  We have since learned that there is another way.  An undocumented
  41.  286 instruction, LOADALL, allows all of the processor's registers (including
  42.  protected mode registers and hidden internal registers) to be loaded, even
  43.  when operating in real mode.  By changing the value of the descriptor
  44.  cache base register, a program can select a segment beyond the lower 1Mbyte.
  45.  LOADALL also has other uses, as described later in this article.
  46.  
  47.  
  48. ----------------------------------------------------------------------
  49.  Physical Memory Address    CPU register
  50.         (hex)
  51.  ======================================================================
  52.  800-805                    none
  53.  806-807                    MSW (Machine Status Word)
  54.  808-815                    None
  55.  816-817                    TR (Task Register)
  56.  818-819                    Flag Word
  57.  81A-81B                    IP (Instruction Pointer)
  58.  81C-81D                    LDT (Local Descriptor Table)
  59.  81E-81F                    DS (Data Segment Selector)
  60.  820-821                    SS (Stack Segment Selector)
  61.  822-823                    CS (Code Segment Selector)
  62.  824-825                    ES (Extra Segment Selector)
  63.  826-827                    DI (Destination Index)
  64.  818-829                    SI (Source Index)
  65.  82A-82B                    BP (Base Pointer)
  66.  82C-82D                    SP (Stack Pointer)
  67.  82E-82F                    BX (Data Register B)
  68.  830-831                    DX (Data Register D)
  69.  832-833                    CX (Data Register C)
  70.  834-835                    AX (Accumulator)
  71.  836-83B                    ES Descriptor Cache
  72.  83C-841                    CS Descriptor Cache
  73.  842-847                    SS Descriptor Cache
  74.  848-84D                    DS Descriptor Cache
  75.  84E-853                    GDTR (Global Descriptor Table Register)
  76.  854-859                    LDT Descriptor Cache
  77.  85A-85F                    IDTR (Interrupt Descriptor Table Register)
  78.  860-865                    TSS (Task State Segment) Descriptor Cache
  79.  =======================================================================
  80.  
  81.  
  82.  Table 1.  LOADALL data area format
  83.  -----------------------------------------------------------------------
  84.  
  85.  Originally included by Intel for chip testing, Microsoft is now using this
  86.  instruction in their RAM Drive program and in OS/2's compatibility box.
  87.  While this instruction is probably appropriate only for use in operating
  88.  systems and system-level utilities, it is important because it provides a
  89.  set of capabilities that are not otherwise available in a 286-based system.
  90.  
  91.  We have received a copy of a document that describes LOADALL.  No company
  92.  name is shown, but is almost surely written by Intel.  The 15-page
  93.  document describes in detail how to use the instruction, so calling it
  94.  "undocumented" is not quite correct -- it is documented, but not in the
  95.  data sheet.  By restricting access to this documentation, Intel gives unfair
  96.  advantage to Microsoft and other large customers.
  97.  
  98.  ----------------------------------------------------------------------
  99.  Descriptor Cache Format
  100.   Bytes 0-2      24-bit segment base address
  101.   Byte 3         Access rights byte.  Format is the same as the access
  102.                  rights byte in a descriptor, except that the "present"
  103.                  bit becomes a "valid" bit.  If a "valid" bit is not
  104.                  set, any memory reference using the descriptor will
  105.                  cause exception 13 with error code of 0.
  106.  Bytes 4-5       16-bit segment size
  107.  
  108.  GDTR and IDTR Format
  109.   Bytes 0-2      24-bit base address
  110.   Byte 3         0
  111.   Bytes 4-5      16-bit segment limit
  112.  ----------------------------------------------------------------------
  113.  Table 2.  Descriptor Cache Formats
  114.  
  115.  Intel's position is that this instruction is not useful to most users,
  116.  and can easily lead to machine crashes if not used properly.  Intel says
  117.  that they will provide documentation for the instruction on a "need-to-know"
  118.  basis, presumably only after a non-disclosure agreement has been signed.
  119.  Most developers, of course, would not know to ask.  The information we are
  120.  presenting here should be enough for you to evaluate the usefulness of the
  121.  instruction; if you intend to use it in a product, you should contact
  122.  Intel for the full documentation.
  123.  
  124.  The opcode for LOADALL is 0F05 hex.  No operands follow the instruction;
  125.  LOADALL gets its data from a 102-byte block of memory starting at the
  126.  fixed address 800 hex, as shown in Table 1.  Table 2 shows the format of
  127.  the descriptor cache entries.  These are hidden registers that cannot
  128.  otherwise be modified by the programmer; they are set automatically when
  129.  a descriptor is read from the descriptor table.
  130.  
  131.  ACCESSING EXTENDED MEMORY FROM REAL MODE
  132.  
  133.  The following sequence is required to access high memory from real mode
  134.  using LOADALL:
  135.    1. Disable interrupts
  136.    2. Save the 102 (decimal) bytes starting at 800 hex.  (MS-DOS uses this
  137.       area for system code.  OS/2 presumably leaves it free for LOADALL,
  138.       but depends on application programs not to change it.)
  139.    3. Set up the 102-byte register image at 800.  The base address in the
  140.       data segment descriptor cache is set to select the desired high-memory
  141.       area.
  142.    4. Execute the LOADALL instruction.  The data segment now points to the
  143.       new, high-memory segment.
  144.    5. Move data to or from high memory.
  145.    6. Restore the base address in the data segment descriptor cache in the
  146.       image at 800, and execute another LOADALL.
  147.    7. Enable interrupts.
  148.    <<< The next step is not in the article, but conspicuously absent. >>>
  149.    8. Restore the original 102 bytes to 800.
  150.  
  151.  Thus while it is appealing not to have to go to protected mode and back to
  152.  access high memory from real mode, this isn't much better.  LOADALL requires
  153.  195 clock cycles on a machine with no wait states, or 19.5 us at 10Mhz.
  154.  
  155.  LOADALL can also be used to allow programs to be executed in high memory,
  156.  even though the processor is in real mode.  A special paragraph ID (such as
  157.  FFFF) is used to indicate when a program is running in extended memory.
  158.  Interrupt service routines must check the segment register to see if any
  159.  contain the special ID, and if so, they must reload the registers using
  160.  LOADALL to restore the segment base addresses before returning from the
  161.  interrupt.
  162.  
  163.  
  164. OTHER APPLICATIONS
  165.  
  166.  LOADALL has other uses as well.  It can be used to switch back to a
  167.  protected mode task from real mode, serving as a fast intermode context
  168.  switch.  OS/2 presumably uses it to return to protected mode from a
  169.  real-mode task running in the compatibility box.
  170.  
  171.  LOADALL can also be used to emulate real mode from protected mode.  By
  172.  setting the privilege level of all local and global descriptor table
  173.  entries lower than that of the current program, a protection violation
  174.  occurs whenever the program attempts to load a segment register.  (Actually,
  175.  loading the segment register with a value of 0000 to 0003 does not cause
  176.  an exception, and this case must be handled specially.)  The exception
  177.  handler then uses LOADALL to set the base address for the segment.
  178.  
  179.  There are many other complexities to this emulation, as described in the
  180.  LOADALL document.  The emulation is imperfect, and Microsoft apparently
  181.  decided to use the "reset to real mode" technique (as described in our
  182.  article last month on the P9), rather than trying to emulate real mode
  183.  from within protected mode.
  184.  
  185.  RESTRICTIONS
  186.  
  187.  LOADALL performs no checking on the values loaded into the registers, so
  188.  no exception will occur even if an illegal value is loaded.  Thus, the
  189.  processor can potential be put into a strange state.  If an illegal
  190.  descriptor value is set, no exception occurs from the execution of LOADALL.
  191.  An exception will occur, however, when an access  using that descriptor
  192.  is attempted.
  193.  
  194.  LOADALL can be executed in protected mode, but only at the most privileged
  195.  level (level 0).  Thus it does not violate the protection.  Unfortunately,
  196.  LOADALL cannot be used to switch back to real mode from protected mode.
  197.  
  198.  Early versions of 286 (A1 and B1 steppings) have bugs which affect the use
  199.  of LOADALL.  Thus, the techniques described in this article may not work
  200.  properly on older systems.
  201.  
  202.  LOADALL is not implemented on the 80386, so Microsoft has included code in
  203.  OS/2 and in the current version of RAM Drive that tests for the processor
  204.  type.  If it's a 286, LOADALL is used, and if it's a 386, the native 386
  205.  mechanisms are used.  If you use LOADALL, you should also perform this
  206.  check, or your software will not run on 386-based machines.
  207.  
  208.  Using LOADALL is certainly fraught with peril, and requires careful
  209.  program design and knowledge of all the implications.  We have spoken with
  210.  a number of software vendors that were aware of the instruction, and all
  211.  had decided not to use it due to the risks and complexity involved.  We
  212.  don't recommend its use to most people, but we do feel that everyone should
  213.  have access to the information so they can make their own choice about
  214.  whether or not to use the instruction.
  215.  
  216.  --
  217.  USPS: The Consultants' Exchange, PO Box 12100, Santa Ana, CA  92712
  218.  TELE: (714) 842-6348: BBS (N81); (714) 842-5851: Xenix guest account (E71)
  219.  UUCP: conexch Any ACU 2400 17148425851 ogin:-""-ogin:-""-ogin: nuucp
  220.  UUCP: ...!uunet!turnkey!conexch!root || ...!trwrb!ucla-an!conexch!root
  221.  
  222.